<?php

namespace App\Jobs\Excel;

use Maatwebsite\Excel\Concerns\WithStyles;
use Maatwebsite\Excel\Concerns\FromCollection;
use PhpOffice\PhpSpreadsheet\Worksheet\Worksheet;

/**
 * demo
 */
## Controller 中调用方式
## use Maatwebsite\Excel\Facades\Excel;
## [return Excel::download(new ProjectTeamExport($list), '团队成员信息汇总表.xls');]
class ProjectExport implements FromCollection, WithStyles
{
    private array $row;
    private array $data;

    public function __construct(array $data)
    {
        $this->data = $data;
        $this->row  = $this->rows();
    }

    /**
     * 设置表头
     * @return array[]
     */
    public function rows()
    {
        return [
            [
                "附件3", "“SKSUNNY幸福行动”XX高校团队信息汇总表", null, null, null, null, null, null, null, null, null, null, null
            ],
            [
                "类别", "序号", "团队名称", "学校名称", "实践地区有关信息", null, null, null, null, "活动物资包接收地址", null, null, null
            ],
            [
                null, null, null, null, "服务地区所属省份", "服务地区所属市名", "服务地区所属县名", "队长姓名", "队长联系方式", "接收人姓名", "接收人联系方式", "活动物资快递地址", "团队人数"
            ],
        ];
    }

    public function collection()
    {
        $row  = $this->row;
        $data = $this->data;

        // 设置数据
        $data_new = [];
        foreach ($data as $key=>$item){
            $data_new[] = [
                $item['type']['name'] ?? '',
                $key + 1,
                $item['apply']['team_name'] ?? '',
                $item['apply']['school_name'] ?? '',
                $item['apply']['province']['name'] ?? '',
                $item['apply']['practice_area'] ?? '',
                $item['apply']['practice_area'] ?? '',
                $item['apply']['team_captain'] ?? '',
                $item['apply']['team_captain_phone'] ?? '',
                $item['apply']['team_supplies_person'] ?? '',
                $item['apply']['team_supplies_phone'] ?? '',
                $item['apply']['team_supplies_address'] ?? '',
                $item['apply']['team_size'] ?? '',
            ];
        }

        $row = array_merge($row,$data_new);

        return collect($row);
    }

    public function styles(Worksheet $sheet)
    {
        // 合并单元格
        $sheet->mergeCells('B1:M1');
        $sheet->mergeCells('A2:A3');
        $sheet->mergeCells('B2:B3');
        $sheet->mergeCells('C2:C3');
        $sheet->mergeCells('D2:D3');
        $sheet->mergeCells('E2:I2');
        $sheet->mergeCells('J2:M2');
        // 设置列宽
        $sheet->getColumnDimension('A')->setWidth(8);
        $sheet->getColumnDimension('B')->setWidth(8);
        $sheet->getColumnDimension('C')->setWidth(10);
        $sheet->getColumnDimension('D')->setWidth(10);
        $sheet->getColumnDimension('E')->setWidth(17);
        $sheet->getColumnDimension('F')->setWidth(17);
        $sheet->getColumnDimension('G')->setWidth(17);
        $sheet->getColumnDimension('H')->setWidth(10);
        $sheet->getColumnDimension('I')->setWidth(15);
        $sheet->getColumnDimension('J')->setWidth(12);
        $sheet->getColumnDimension('K')->setWidth(15);
        $sheet->getColumnDimension('L')->setWidth(18);
        $sheet->getColumnDimension('M')->setWidth(10);

        // 设置垂直剧中
        $sheet->getStyle('B1:M1')->getAlignment()->setVertical('center');//垂直居中
        $sheet->getStyle('A2:A3')->getAlignment()->setVertical('center');//垂直居中
        $sheet->getStyle('B2:B3')->getAlignment()->setVertical('center');//垂直居中
        $sheet->getStyle('C2:C3')->getAlignment()->setVertical('center');//垂直居中
        $sheet->getStyle('D2:D3')->getAlignment()->setVertical('center');//垂直居中
        $sheet->getStyle('E2:I2')->getAlignment()->setVertical('center');//垂直居中
        $sheet->getStyle('J2:M2')->getAlignment()->setVertical('center');//垂直居中

        $sheet->getStyle('A:M')->applyFromArray([
            'alignment' => ['horizontal' => 'center'],//左右居中
            'font' => [
                'name' => 'Calibrin',
                'size' => 10,
                'bold' => false,
                'italic' => false,
                'strikethrough' => false,
                'color' => [
                    'rgb' => '000000'
                ]
            ],
        ]);


//        // 设置单元格的值
//        $sheet->getCell('A1')->setValue("填写须知：\n 1. 请勿修改表格结构；\n 2. 标红字段为必填项，黑色字段为选填项；");
//        // 设置字体颜色
//        $sheet->getStyle('A5')->getFont()->getColor()->setARGB(\PhpOffice\PhpSpreadsheet\Style\Color::COLOR_RED);
//        $sheet->getStyle('B5')->getFont()->getColor()->setARGB(\PhpOffice\PhpSpreadsheet\Style\Color::COLOR_RED);
//        $sheet->getStyle('F')->getFont()->getColor()->setARGB(\PhpOffice\PhpSpreadsheet\Style\Color::COLOR_RED);
//        // 设置背景色
//        $sheet->getStyle('A5:E5')->getFill()
//            ->setFillType(\PhpOffice\PhpSpreadsheet\Style\Fill::FILL_SOLID)
//            ->getStartColor()->setRGB('e9e9e9');
//        // 设置列宽
//        $sheet->getColumnDimension('A')->setWidth(20);
//        $sheet->getColumnDimension('B')->setWidth(20);
//        $sheet->getColumnDimension('C')->setWidth(20);
//        $sheet->getColumnDimension('D')->setWidth(30);
//        $sheet->getColumnDimension('E')->setWidth(20);
//        $sheet->getColumnDimension('F')->setWidth(20);
//        // 设置行高
//        $sheet->getRowDimension('1')->setRowHeight(100);
//        $sheet->getRowDimension('2')->setRowHeight(1);
//        $sheet->getRowDimension('3')->setRowHeight(1);
//        $sheet->getRowDimension('4')->setRowHeight(1);
//        // 设置默认行高
//        $sheet->getDefaultRowDimension()->setRowHeight(17);
//        // setWrapText自动换行，setVertical垂直对齐方式
//        $sheet->getStyle('A1')->getAlignment()->setVertical(\PhpOffice\PhpSpreadsheet\Style\Alignment::VERTICAL_TOP)->setWrapText(true);
    }
}